{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Series",
   "id": "5947ac02e07a923d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:42:07.454451Z",
     "start_time": "2025-01-13T10:42:07.098293Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成一个Series\n",
    "import pandas as pd\n",
    "ser_obj = pd.Series(range(10, 20)) #默认索引是0-9\n",
    "print(ser_obj) #打印输出会带有类型\n",
    "\n",
    "print('-'*50)\n",
    "# 获取数据\n",
    "print(ser_obj.values)  #values实际是ndarray\n",
    "print(type(ser_obj.values)) #类型是ndarray\n",
    "# 获取索引\n",
    "print(ser_obj.index)  #内部自带的类型--RangeIndex\n",
    "ser_obj.dtype #数据类型"
   ],
   "id": "d9efbc4bfb57260",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n",
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:43:00.694783Z",
     "start_time": "2025-01-13T10:43:00.689898Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj[0]) \n",
    "ser_obj[9] #\n",
    "# 访问不存在的索引下标会报keyerror\n",
    "\n",
    "print(ser_obj * 2)  #元素级乘法,每个数值都乘以2\n",
    "print(ser_obj > 15) #返回一个bool序列"
   ],
   "id": "e08dda93813f93d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int64(19)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:44:46.542330Z",
     "start_time": "2025-01-13T10:44:46.534928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#字典变为series，索引是字典的key，value是字典的value，感受非默认索引\n",
    "\n",
    "year_data = {2001: 17.8, 2005: 20.1, 2003: 16.5}  #字典\n",
    "ser_obj2 = pd.Series(year_data)  #字典变为series\n",
    "print(ser_obj2) \n",
    "print('-'*50)\n",
    "print(ser_obj2.index)\n",
    "print('-'*50)\n",
    "print(ser_obj2[2001])  #通过索引访问值\n",
    "ser_obj2.values"
   ],
   "id": "b455afc26adba7a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "--------------------------------------------------\n",
      "17.8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([17.8, 20.1, 16.5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:46:53.662919Z",
     "start_time": "2025-01-13T10:46:53.658586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#有点鸡肋\n",
    "print(ser_obj2.name) #Series名字，默认是None\n",
    "print(ser_obj2.index.name)  #索引名字，默认是None\n",
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year1'\n",
    "print('-'*50)\n",
    "print(ser_obj2.head())  #head默认显示前5行"
   ],
   "id": "ebfee0ec6983f351",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "None\n",
      "--------------------------------------------------\n",
      "year1\n",
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "Name: temp, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFrame",
   "id": "37de49c892022f6e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:48:15.288153Z",
     "start_time": "2025-01-13T10:48:15.282582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 通过ndarray构建DataFrame\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4))) #默认索引是0-2，默认列索引是0-3\n",
    "print(t)\n",
    "print('-'*50)"
   ],
   "id": "2e5d8875ae7afd52",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:50:04.480186Z",
     "start_time": "2025-01-13T10:50:04.474368Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array = np.random.randn(5,4)  #随机生成5行4列的ndarray\n",
    "print(array)\n",
    "print('-'*50)\n",
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj.head()) #默认显示前5行"
   ],
   "id": "2dfa0913880398b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.90801645 -0.07609413 -1.55513012  0.12030343]\n",
      " [ 0.15708654 -1.49859583 -0.55122026  1.3400865 ]\n",
      " [ 0.23148988  1.38101004 -0.91126395 -0.03678959]\n",
      " [ 0.13271087  0.15614663  0.00485425  1.42256116]\n",
      " [ 1.1689338  -0.80744273 -0.12969037 -0.96432433]]\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -0.908016 -0.076094 -1.555130  0.120303\n",
      "1  0.157087 -1.498596 -0.551220  1.340086\n",
      "2  0.231490  1.381010 -0.911264 -0.036790\n",
      "3  0.132711  0.156147  0.004854  1.422561\n",
      "4  1.168934 -0.807443 -0.129690 -0.964324\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:51:05.318001Z",
     "start_time": "2025-01-13T10:51:05.311636Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t.loc[0]) #单独把某一行取出来,类型是series\n",
    "print(t.loc[1])"
   ],
   "id": "2bd9742bbf4a3814",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "Name: 0, dtype: int64\n",
      "0    4\n",
      "1    5\n",
      "2    6\n",
      "3    7\n",
      "Name: 1, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:53:04.143500Z",
     "start_time": "2025-01-13T10:53:04.135545Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列表套字典  变df\n",
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},\n",
    "     { \"name\": \"xiaogang\" ,\"tel\": 10000} ,\n",
    "     {\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "df6=pd.DataFrame(d2)\n",
    "print(df6) #缺失值会用NaN填充\n",
    "print(type(df6.values)) #ndarray"
   ],
   "id": "a4b8adf39927bc54",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T10:54:30.176059Z",
     "start_time": "2025-01-13T10:54:30.170709Z"
    }
   },
   "cell_type": "code",
   "source": "pd.Series(1, index=list(range(3,7)),dtype='float32') #指定索引和数据类型",
   "id": "6296f14404423208",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    1.0\n",
       "4    1.0\n",
       "5    1.0\n",
       "6    1.0\n",
       "dtype: float32"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:03:27.904190Z",
     "start_time": "2025-01-13T11:03:27.896947Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#df中不同列可以是不同的数据类型,同一列必须是一个数据类型\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': 1, \n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'), \n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"], \n",
    "             'F': 'wangdao'  }  #一个数据就广播到所有行，不为一个数据就必须是满数据\n",
    "df_obj2 = pd.DataFrame(dict_data) #字典变为df\n",
    "print(df_obj2)"
   ],
   "id": "f5663d6c7059dde",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:06:37.144358Z",
     "start_time": "2025-01-13T11:06:37.140011Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "print(df_obj2.index) #行索引index,重点\n",
    "#不可改变\n",
    "# df_obj2.index[0]=2  不可以单独修改某个索引值\n",
    "print(df_obj2.columns) #列索引columns，重点\n",
    "print(df_obj2.dtypes) #每一列的数据类型，重点,numpy.ndarray类型的字符串类型不是str，是object"
   ],
   "id": "aa740a3bc929f9bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')\n",
      "A            int64\n",
      "B    datetime64[s]\n",
      "C          float32\n",
      "D            int32\n",
      "E           object\n",
      "F           object\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:14:06.414302Z",
     "start_time": "2025-01-13T11:14:06.406607Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 感受日期,初始化df，设置行索引，列索引\n",
    "dates = pd.date_range('20130101', periods=6) #默认freq='D'，即天; periods=6，即6天\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD')) #设置数据，行索引，列索引。list('ABCD')是将字符串‘ABCD’变为列表['A','B','C','D']\n",
    "print(df)\n",
    "print('-'*50)\n",
    "print(df.index)"
   ],
   "id": "1d397528e2c2e45d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2013-01-01 -0.188450  1.001333  0.299952 -0.603862\n",
      "2013-01-02  0.359102 -0.718126  0.987366 -2.471444\n",
      "2013-01-03 -0.798884  0.418261 -0.146230  0.268579\n",
      "2013-01-04 -0.322466  1.497612  1.660950 -0.628125\n",
      "2013-01-05 -0.525367 -0.062759 -2.022630  0.141583\n",
      "2013-01-06 -0.445736 -0.737895  0.559576  1.440188\n",
      "--------------------------------------------------\n",
      "DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',\n",
      "               '2013-01-05', '2013-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:29:41.538210Z",
     "start_time": "2025-01-13T11:29:41.528049Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#取数据\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(type(df_obj2))\n",
    "print('-'*50)\n",
    "#pd中使用索引名来取某一行，或者列\n",
    "print(df_obj2['B'])\n",
    "print('-'*50)\n",
    "#把df的某一列取出来是series,某个值是numpy.ndarray类型\n",
    "print(type(df_obj2['B']))\n"
   ],
   "id": "46dff82ab8cbe6ff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "0   2019-09-26\n",
      "1   2019-09-26\n",
      "2   2019-09-26\n",
      "3   2019-09-26\n",
      "Name: B, dtype: datetime64[s]\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:30:23.136698Z",
     "start_time": "2025-01-13T11:30:23.131334Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#增加列数据，列名是自定义的\n",
    "df_obj2['G'] = df_obj2['D'] + 4\n",
    "print(df_obj2.head())"
   ],
   "id": "b058c1d144468453",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao  5\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao  6\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  1 2019-09-26  1.0  4       C  wangdao  8\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T11:30:51.404034Z",
     "start_time": "2025-01-13T11:30:51.398764Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除列\n",
    "del(df_obj2['G'])\n",
    "print(df_obj2.head())"
   ],
   "id": "f3ad946ad2a174e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
